﻿Public Partial Class Detail_VesselBerth_Popup
    Inherits System.Web.UI.Page

    Dim clsGet As New ClassFunctionGetData
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not (IsPostBack) Then
            If Not (Session("Booking_no_Detail") Is Nothing) Then
                Dim Booking_no As String = Session("Booking_no_Detail")
                ViewState("PORT") = clsGet.Get_BookingOneRecord(Booking_no).Tables(0).Rows(0).Item("PORT") & ""
                Dim dt As DataTable = clsGet.Get_Booking_LogSheet_In(Booking_no)
                If (dt.Rows.Count > 0) Then
                    lbETA.Text = dt.Rows(0).Item("ETA_Date") & " " & dt.Rows(0).Item("ETA_Time")
                    lbNOR.Text = dt.Rows(0).Item("NOR_Date") & " " & dt.Rows(0).Item("NOR_Time")
                    lbBerth.Text = dt.Rows(0).Item("Berthing_Date") & " " & dt.Rows(0).Item("Berthing_TimeFrom") & "-" & dt.Rows(0).Item("Berthing_TimeTo")
                    lbPOB.Text = dt.Rows(0).Item("POB_Date") & " " & dt.Rows(0).Item("POB_Time")
                    lbAwake.Text = dt.Rows(0).Item("Up_Date") & " " & dt.Rows(0).Item("Up_Time")
                Else
                    lbAwake.Text = ""
                    lbETA.Text = ""
                    lbNOR.Text = ""
                    lbBerth.Text = ""
                    lbPOB.Text = ""
                End If
                dt = clsGet.Get_BookingOneRecord(Booking_no).Tables(0)
                If (dt.Rows.Count > 0) Then
                    If (dt.Rows(0).Item("Date_From") & "" = dt.Rows(0).Item("Date_To") & "") Then
                        lbLayCan.Text = dt.Rows(0).Item("Date_To")
                    Else
                        lbLayCan.Text = dt.Rows(0).Item("Date_From").ToString.Substring(0, 2) & "-" & dt.Rows(0).Item("Date_To")
                    End If
                    If (dt.Rows(0).Item("Lay_Time") & "" <> "0") And (dt.Rows(0).Item("Lay_Time") & "" <> "") Then
                        lbLayTime.Text = dt.Rows(0).Item("Lay_Time") & " hours"
                    Else
                        lbLayTime.Text = "0 hours"
                    End If
                Else
                    lbLayCan.Text = ""
                    lbLayTime.Text = ""
                End If
                dt = clsGet.Get_Load_Unload_Vessel_Product_Detail(Booking_no)
                Dim dx As DataTable = CreateDataset()
                If (dt.Rows.Count > 0) Then
                    For Each dr As DataRow In dt.Rows
                        Try
                            Dim Product_Name As String = ""
                            Dim Connect As String = ""
                            Dim starts As String = ""
                            Dim Ends As String = ""
                            Dim Disconnect As String = ""
                            If (dr.Item("Product_Name") & "" <> "") Then
                                Product_Name = dr.Item("Product_Name") & ""
                            End If
                            If (dr.Item("LU_Connect_Date") & "" <> "") Then
                                Connect = dr.Item("LU_Connect_Date").ToString.Substring(0, 2) & " " & GetNameMonth(dr.Item("LU_Connect_Date").ToString.Substring(3, 2)) & " 20" & dr.Item("LU_Connect_Date").ToString.Substring(6, 2) & " " & dr.Item("LU_Connect_From") & "-" & dr.Item("LU_Connect_To")
                            End If
                            If (dr.Item("LU_Disconnect_Date") & "" <> "") Then
                                Disconnect = dr.Item("LU_Disconnect_Date").ToString.Substring(0, 2) & " " & GetNameMonth(dr.Item("LU_Disconnect_Date").ToString.Substring(3, 2)) & " 20" & dr.Item("LU_Disconnect_Date").ToString.Substring(6, 2) & " " & dr.Item("LU_Disconnect_From") & "-" & dr.Item("LU_Disconnect_To")
                            End If
                            If (dr.Item("LU_StartActual_Date") & "" <> "") Then
                                starts = dr.Item("LU_StartActual_Date").ToString.Substring(0, 2) & " " & GetNameMonth(dr.Item("LU_StartActual_Date").ToString.Substring(3, 2)) & " 20" & dr.Item("LU_StartActual_Date").ToString.Substring(6, 2) & " " & dr.Item("LU_StartActual_Time")
                            End If
                            If (dr.Item("LU_StopActual_Date") & "" <> "") Then
                                Ends = dr.Item("LU_StopActual_Date").ToString.Substring(0, 2) & " " & GetNameMonth(dr.Item("LU_StopActual_Date").ToString.Substring(3, 2)) & " 20" & dr.Item("LU_StopActual_Date").ToString.Substring(6, 2) & " " & dr.Item("LU_StopActual_Time")
                            End If
                            dx = AddDatarow(dx, Product_Name, Connect, starts, Ends, Disconnect)
                        Catch ex As Exception

                        End Try

                    Next
                Else
                    If (ViewState("PORT") & "" = "B") Then
                        dt = clsGet.Get_Load_Unload_Product_Bulk(Booking_no, "", "")
                        If (dt.Rows.Count > 0) Then
                            For Each dr As DataRow In dt.Rows
                                Try
                                    Dim Product_Name As String = ""
                                    Dim Connect As String = ""
                                    Dim starts As String = ""
                                    Dim Ends As String = ""
                                    Dim Disconnect As String = ""
                                    If (dr.Item("Product_No") & "" <> "") Then
                                        Product_Name = dr.Item("Product_No") & ""
                                    End If
                                    If (dr.Item("_Date") & "" <> "") Then
                                        Connect = dr.Item("_Date").ToString & " " & dr.Item("_Time")
                                    End If
                                    If (dr.Item("_Date2") & "" <> "") Then
                                        Disconnect = dr.Item("_Date2").ToString & " " & dr.Item("_Time2")
                                    End If
                                    dx = AddDatarow(dx, Product_Name, Connect, Connect, Disconnect, Disconnect)
                                Catch ex As Exception

                                End Try

                            Next
                        Else
                            dx = AddDatarow(dx, "", "", "", "", "")
                        End If
                    Else
                        dx = AddDatarow(dx, "", "", "", "", "")
                    End If

                End If
                gridProduct.DataSource = dx
                gridProduct.DataBind()

                dt = clsGet.Get_Load_Unload_Vessel(Booking_no)
                If (dt.Rows.Count > 0) Then
                    Dim Actual_Date As String = ""
                    If (dt.Rows(0).Item("Depart_Actual_Date") & "" <> "") Then
                        Dim Value As String = dt.Rows(0).Item("Depart_Actual_Date") & ""
                        Actual_Date = Value.Substring(0, 2) & " " & GetNameMonth(Value.Substring(3, 2)) & " 20" & Value.Substring(6, 2)
                    End If
                    Dim NOR_Date As String = ""
                    If (dt.Rows(0).Item("NOR_Date") & "" <> "") Then
                        Dim Value As String = dt.Rows(0).Item("NOR_Date") & ""
                        NOR_Date = Value.Substring(0, 2) & " " & GetNameMonth(Value.Substring(3, 2)) & " 20" & Value.Substring(6, 2)
                    End If
                    lbLCT.Text = Actual_Date & " " & dt.Rows(0).Item("Depart_Actual_Time")
                    lbNORPORT.Text = NOR_Date & " " & dt.Rows(0).Item("NOR_Time")
                Else
                    lbLCT.Text = ""
                    lbNORPORT.Text = ""
                End If

                dt = clsGet.Get_Booking_LogSheet_Out(Booking_no)
                If (dt.Rows.Count > 0) Then
                    lbPTO.Text = dt.Rows(0).Item("Last_Date") & " " & dt.Rows(0).Item("Last_Time")
                Else
                    lbPTO.Text = ""
                End If
                If ViewState("PORT") & "" = "L" Then
                    rowLCT.Visible = True
                Else
                    rowLCT.Visible = False
                End If
            End If
        End If
    End Sub

    Private Function CreateDataset()
        Dim dt As New DataTable
        dt.Columns.Add("Product_name", GetType(String))
        dt.Columns.Add("Connect", GetType(String))
        dt.Columns.Add("Start", GetType(String))
        dt.Columns.Add("Stop", GetType(String))
        dt.Columns.Add("Disconnect", GetType(String))
        dt.AcceptChanges()
        Return dt
    End Function

    Private Function AddDatarow(ByVal dt As DataTable, ByVal Product_Name As String, ByVal Connect As String, ByVal Start As String, ByVal Stops As String, ByVal Disconnect As String) As DataTable
        Dim dr As DataRow = dt.NewRow()
        dr.Item("Product_name") = Product_Name
        dr.Item("Connect") = Connect
        dr.Item("Start") = Start
        dr.Item("Stop") = Stops
        dr.Item("Disconnect") = Disconnect

        dt.Rows.Add(dr)
        dt.AcceptChanges()
        Return dt
    End Function

    Private Sub btnClose_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Session("Booking_no_Detail") = Nothing
        ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " parent.closeModal('@');", True)
    End Sub

    Private Function GetNameMonth(ByVal Month As String) As String
        Try
            Select Case (CInt(Month).ToString("00"))
                Case "01"
                    Return "Jan"
                Case "02"
                    Return "Feb"
                Case "03"
                    Return "Mar"
                Case "04"
                    Return "Apr"
                Case "05"
                    Return "May"
                Case "06"
                    Return "Jun"
                Case "07"
                    Return "Jul"
                Case "08"
                    Return "Aug"
                Case "09"
                    Return "Sep"
                Case "10"
                    Return "Oct"
                Case "11"
                    Return "Nov"
                Case "12"
                    Return "Dec"
                Case Else
                    Return ""
            End Select
        Catch ex As Exception
            Return Month
        End Try

    End Function

    Private Sub gridProduct_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gridProduct.RowDataBound
        If (e.Row.RowType = DataControlRowType.DataRow) Then
            Dim rowCOnnect As HtmlControls.HtmlTableRow = CType(e.Row.FindControl("rowCOnnect"), HtmlControls.HtmlTableRow)
            Dim rowDisconnect As HtmlControls.HtmlTableRow = CType(e.Row.FindControl("rowDisconnect"), HtmlControls.HtmlTableRow)
            Dim Label7Start As Label = CType(e.Row.FindControl("Label7Start"), Label)
            Dim Label9Stop As Label = CType(e.Row.FindControl("Label9Stop"), Label)
            If ViewState("PORT") & "" = "B" Then
                rowCOnnect.Visible = False
                rowDisconnect.Visible = False
                Label7Start.Text = "COMMEND :"
                Label9Stop.Text = "COMPLETED :"
            Else
                rowCOnnect.Visible = True
                rowDisconnect.Visible = True
                Label7Start.Text = "STARTED S/L,S/U :"
                Label9Stop.Text = "STOPPED S/L,S/U :"
            End If
        End If
    End Sub

    Private Sub MainPage_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
        Dim ds As DataSet = Session("UserData")
        If Not (ds Is Nothing) Then
            Page.MasterPageFile = clsGet.GetMaster_Page(ds.Tables(0).Rows(0).Item("User_Name").ToString(), "")
        End If
    End Sub
End Class